#include "server.h"
#include "util.h"
#include "log.h"
#include <glib.h>
#include <sys/types.h>
#include <sys/wait.h>

#define VERSION "0.0.1"

#if 0
static gboolean verbose = FALSE;
static gboolean enable_debug = FALSE;
static gchar* startup_cmd = NULL;

static GOptionEntry entries[] = {
    {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "verbose...", NULL},
    {"debug", 'd', G_OPTION_ARG_NONE, &enable_debug, "debug...", NULL},
    {"startup", 's', G_OPTION_ARG_STRING, &startup_cmd, "startup...", NULL},
    { NULL }
};
#endif

int main(int argc, char** argv)
{
#if 0
    char *startup_cmd = NULL;
    int c;

    while (1) {
        c = getopt(argc, argv, "s:hv");
        if (c == 's')
            startup_cmd = optarg;
        else if (c == 'v')
            die("bwl " VERSION);
        else
            goto usage;
    }
    if (optind < argc)
        goto usage;
#endif

    gboolean verbose = FALSE;
    gboolean enable_debug = FALSE;
    gchar* startup_cmd = NULL;

    GOptionEntry entries[] = {
        {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "verbose...", NULL},
        {"debug", 'd', 0, G_OPTION_ARG_NONE, &enable_debug, "debug...", NULL},
        {"startup", 's', 0, G_OPTION_ARG_STRING, &startup_cmd, "startup...", NULL},
        { NULL }
    };

    GError* error = NULL;

#if 1
    GOptionContext *context = g_option_context_new("option...");
    g_option_context_add_main_entries(context, entries, NULL);
    // g_option_context_add_group(context, NULL);
    if (!g_option_context_parse(context, &argc, &argv, &error)) {
        g_printerr("option parsed failed: %s\n", error->message);
    }
    g_option_context_free(context);

    if (verbose) {
        g_print("bwl " VERSION "\n");
        return 0;
    }

    if (enable_debug) {
        log_init();
    }
#endif
    /* Wayland requires XDG_RUNTIME_DIR for creating its communications socket */
    if (!getenv("XDG_RUNTIME_DIR"))
        die("XDG_RUNTIME_DIR must be set");
//    setup();

    Server s;
    s.run(startup_cmd);
    return EXIT_SUCCESS;

// usage:
    // die("Usage: %s [-v] [-s startup command]", argv[0]);
}
